<?php
header('Content-type:text/html;charset=utf-8');
session_start();
include_once("../class/mysqlclass.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (empty(Addslashes(trim($_POST['username'])))){
        echo "<script>alert('用户名不能为空！');location.href='../html/register.html';</script>";
    }else {
        $username = trim($_POST['username']);
    }
    if (empty(trim($_POST['password']))){
        echo "<script>alert('密码不能为空！');location.href='../html/register.html';</script>";
    }else{
        $password = $_POST['password'];
    }
    if (empty(trim($_POST['repassword']))){
        echo "<script>alert('确认密码不能为空！');location.href='../html/register.html';</script>";
    }else{
        $repassword = $_POST['repassword'];
    }
    if ($password != $repassword) {
        echo "<script>alert('两次输入密码不一致！');location.href='../html/register.html';</script>";
    }
}
$mysqli = new test;
$mysqli = $mysqli -> Con();
$usn = $_POST['username'];
$psw = $_POST['password'];
//用户存在则不能插入，否则注册成功
$sql = "INSERT INTO `user` (`id`, `username`, `password`, `telephone`, `age`, `photo`, `type`) VALUES (NULL, '{$usn}', '{$psw}', NULL, NULL, NULL, NULL);";
$res=$mysqli->query($sql);
if($res == 0){
    echo "<script>alert('用户已存在！');location.href='../html/register.html';</script>";
}else {
	echo "<script>alert('注册成功！返回登录页面');location.href='../html/login.html';</script>";
	$_SESSION['tph']= NULL;
	$_SESSION['age']= NULL;
	$_SESSION['ath']= $usn;
	$_SESSION['tle']= NULL;
    
}